/**
 ********************************************************************
 * @author roocket
 * @file    main.c
 * @version V0.0.0
 * @date    2024/3/22
 * @brief   This file is to indicate main functions. 
 * @attention Code file rules:
 * rule: file encoding use UTF8;
 * rule: max line length 120 characters;
 * rule: line separator \r\n;
 * rule: use clion auto code format tool.
 */

#include "file_synchronize.h"
#include "file_crypt.h"
#include "file_lock.h"

int test_what_to_do(void *p)
{
	printf("the string is %s\n", (char *)p);
	return 0;
}

int main(void)
{
	int res = -1;
	int fd;
	char test_string[32] = "hello file lock";

	fd = file_lock_init_key_of_door("test.txt");
	if (-1 == fd) {
		perror("file lock init key-of-door error");
		return -1;
	}

	res = file_lock_trywait_acquire(fd, F_WRLCK, test_what_to_do, (void*)test_string);
	if (-1 == res) {
		perror("file lock trywait error");
		return -1;
	}
	sleep(10);

	res = file_lock_unlock_door(fd);
	if (-1 == res) {
		perror("file lock unlock door error");
		return -1;
	}
    return 0;
}
